﻿#include "CyButton1.h"
#include "resource.h"
using namespace std;

CyButton1::CyButton1(HWND hWnd) : _client(hWnd)
{
	count = 0;
	count2 = 0;

	_txtX = new CyGUIText(hWnd, TXT_X);
	_txtY = new CyGUIText(hWnd, TXT_Y);
	_txtZ = new CyGUIText(hWnd, TXT_Z);
	_txtMag = new CyGUIText(hWnd, TXT_MAG);

	_button = new CyGUIButton(hWnd, BTN_BUTTON1);
	_button->SetClickHandler<CyButton1>(this, &CyButton1::HandleMessage);

	_button2 = new CyGUIButton(hWnd, BTN_BUTTON2);
	_button2->SetClickHandler<CyButton1>(this, &CyButton1::HandleMessage2);

	_edt = new CyGUIEditBox(hWnd, EDT_EDIT1);
	_edt->SetText(L"Nguyễn Ngọc Hùng");
	_edt->SetHandler<CyButton1>(this, &CyButton1::HandleMessage3);
	
	_edtConsole = new CyGUIEditBox(hWnd, EDT_CONSOLE);

	_client.SetRecvHandler<CyButton1>(this, &CyButton1::RecvHandler);
	_client.SetCloseHandler<CyButton1>(this, &CyButton1::CloseHandler);
	//_client.SetSendHandler<CyButton1>(this, &CyButton1::SendHandler);

	_timer = new CyTimer(1000);
	_timer->SetHandler<CyButton1>(this, &CyButton1::TimerHandler);
	//_timer->Start();
}

CyButton1::~CyButton1(void)
{
	
}

// connect
BOOL CyButton1::HandleMessage(HWND hWnd,
	UINT message, WPARAM wParam, LPARAM lParam)
{
	_client.Connect(12345, "192.168.0.100");
//	_client.Connect(12345, "192.168.43.1");
	return TRUE;
}

// disconnect
BOOL CyButton1::HandleMessage2(HWND hWnd,
	UINT message, WPARAM wParam, LPARAM lParam)
{
	_client.Disconnect();

	return TRUE;
}

BOOL CyButton1::HandleMessage3(HWND hWnd,
	UINT message, WPARAM wParam, LPARAM lParam)
{	
	wstring buffer;
	//_client.Send(CyNetText(_edt->GetText(buffer)));
	_edt->SetText(L"");

	_client.Send(CyNetDummy(1*1024));

	return TRUE;
}

BOOL CyButton1::RecvHandler(HWND hWnd, 
	UINT message, WPARAM wParam, LPARAM lParam)
{
	CyNetText * tcpObj = reinterpret_cast<CyNetText *>(lParam);

	switch (tcpObj->GetType())
	{
	case CyNetObjectEnum::TEXT:
		{
			wstring buffer;
			Print(tcpObj->GetText(buffer));
		}
		break;
	
	case CyNetObjectEnum::ACCELEROMETER:
		{
			static float x = 0;
			static float y = 0;
			static float z = 0;
			static float mag = 0;
			static float alpha = 0.99f;

			CyNetAccelerometer * accObj = (CyNetAccelerometer *) (tcpObj);
			wchar_t buffer[100];

			x = (x * alpha) + accObj->_x * (1 - alpha);
			y = (y * alpha) + accObj->_y * (1 - alpha);
			z = (z * alpha) + accObj->_z * (1 - alpha);
			mag = (mag * alpha) + accObj->Magnitude() * (1 - alpha);
			
			swprintf_s(buffer, 100, L"X = %f", x);
			_txtX->SetText(buffer);

			swprintf_s(buffer, 100, L"Y = %f", y);
			_txtY->SetText(buffer);

			swprintf_s(buffer, 100, L"Z = %f", z);
			_txtZ->SetText(buffer);

			swprintf_s(buffer, 100, L"Magnitude = %f\n", mag);
			_txtMag->SetText(buffer);

			OutputDebugString(buffer);
		}
		break;
	}

	delete tcpObj;

	return true;
}

BOOL CyButton1::CloseHandler(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	Print(L"<<<Close connection>>>");

	return true;
}

BOOL CyButton1::TimerHandler(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	wchar_t buffer[10];
	_itow_s(count, buffer, 10, 10);
	_edt->SetText(buffer);
	count++;

	return true;
}

BOOL CyButton1::SendHandler(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	_client.Send(CyNetDummy(1024*1024));

	return true;
}

void CyButton1::Print(std::wstring str)
{
	_console = str + L"\r\n" + _console;
	_edtConsole->SetText(_console);
}